# Contributor: Francesco Colista <fcolista@alpinelinux.org>
# Maintainer: Francesco Colista <fcolista@alpinelinux.org>
pkgname=flannel
_pkgname=flanneld
pkgver=0.18.1
pkgrel=0
pkgdesc="An etcd backed network fabric for containers"
url="https://github.com/coreos/flannel"
arch="x86_64"
license="Apache-2.0"
makedepends="go linux-headers"
subpackages="$pkgname-openrc $pkgname-contrib-cni:contrib_cni:noarch"
source="$pkgname-$pkgver.tar.gz::https://github.com/flannel-io/flannel/archive/refs/tags/v$pkgver.tar.gz
kube-flannel-bin-path.patch
$_pkgname.logrotated
$_pkgname.initd
$_pkgname.confd"
builddir="$srcdir/$pkgname-$pkgver"

export GOPATH="$srcdir"

prepare() {
	default_prepare
	mkdir -p $builddir/out
}

build() {
	export GO111MODULE=on	
	go build \
		-mod=readonly \
		-ldflags "-s -w -X github.com/flannel-io/flannel/version.Version=$pkgver" \
		-o "out/$_pkgname"
}

check() {
	./out/flanneld --version
}

package() {
	install -Dm0755 out/$_pkgname "$pkgdir"/usr/bin/$_pkgname
	install -Dm755 "$srcdir"/$_pkgname.initd \
		"$pkgdir"/etc/init.d/$_pkgname
	install -Dm755 "$srcdir"/$_pkgname.confd \
		"$pkgdir"/etc/conf.d/$_pkgname
	install -Dm644 "$srcdir"/$_pkgname.logrotated \
		"$pkgdir"/etc/logrotate.d/$_pkgname
	install -d "$pkgdir"/var/log/$pkgname
	install -d "$pkgdir"/var/run/$pkgname
}

contrib_cni() {
	pkgdesc="$pkgname contrib cni config files"
	mkdir -p "$subpkgdir"/etc/cni/net.d
	install -Dm0755 "$builddir"/dist/mk-docker-opts.sh "$subpkgdir"/usr/bin/mk-docker-opts.sh
	install -Dm0755 "$builddir"/dist/iptables-wrapper-installer.sh "$subpkgdir"/usr/bin/iptables-wrapper-installer.sh
	install -Dm0644 "$builddir"/Documentation/kube-flannel.yml \
		"$subpkgdir"/etc/kube-flannel/kube-flannel.yml
	install -Dm0644 "$builddir"/Documentation/k8s-manifests/kube-flannel-legacy.yml \
		"$subpkgdir"/etc/kube-flannel/kube-flannel-legacy.tml
}

cleanup_srcdir() {
        export GOPATH="$srcdir"
        go clean -modcache
        default_cleanup_srcdir
}

sha512sums="
3e3cefea01103bed671deabed0f3c5384fd8da014e2a07793d30fd3fa8bbc7ae50b8aaeb892d8bf0e5e0372b2bd9ceafb410ff2a5ef079a3241a9fbb646f7c4e  flannel-0.18.1.tar.gz
398b7068c46b132b747cade43f7f718703f8b32ef7d59fb54da5105ab3fac97b81e0af69e13207ec7cd6fa42a266be8ec45213bf7853fdbc560fbaee4bb5966d  kube-flannel-bin-path.patch
d84258a164adcec0f1cef7fcafcf0cadb3bf3959b393b905c4897274adeff476279ce96ec42e9aa9d574c6bd7d37649d92c7c52563975f85d303cade990a3942  flanneld.logrotated
3f6f33fe90adca1dd17b503ce0cd3a7f3cc0ca7fc10d0ca5b631f4c87ed513c0133dfd7679618cf95540b252b3176f9bbefd6454867ef792258c46cafc039d86  flanneld.initd
353b98bfc1a73d8f691b6f79489e0224961b5af8ca36ce2edde2d7a86790612de1d597c3455222f38a1c9f09b377e1bf3adfeedc04bd6b0b75c653a732d58988  flanneld.confd
"
